﻿
; (function (global) {

    'use strict';
    global.Game = {

        matchModel: null,
        matchTimer: null,
        solvedGrid: null,
        init: function () {
            this.matchTimer = new Timer(this.onTimerTick.bind(this), 10 * 1000);
            EVENTBROKER.gameTopic.on(TOPIC_EVENTS.gameReady, this.onReady.bind(this));
            EVENTBROKER.matchTopic.on(TOPIC_EVENTS.matchRequestNew, this.onRequestNew.bind(this));
            EVENTBROKER.matchTopic.on(TOPIC_EVENTS.matchRequestPause, this.onRequestPause.bind(this));
            EVENTBROKER.matchTopic.on(TOPIC_EVENTS.matchRequestResume, this.onRequestResume.bind(this));
            EVENTBROKER.matchTopic.on(TOPIC_EVENTS.matchRequestQuit, this.onRequestQuit.bind(this));
            EVENTBROKER.matchTopic.on(TOPIC_EVENTS.matchRequestRestart, this.onRequestRestart.bind(this));
            EVENTBROKER.gridTopic.on(TOPIC_EVENTS.gridFill, this.onGridChanged.bind(this));
        },

        onReady: function () {

            var lastMatch = this.loadSavedMatch();
            if (lastMatch) {
                this.startMatch(lastMatch);
            }

            //  this.onRequestNew(null, 'medium');
            //this.startMatch(new MatchModel(GridModel.fromShortString("12-358647678914253453672189762581394915436728384729516897265431531847962246193875")));
        },


        startMatch: function (matchModel) {
            this.matchModel = matchModel;

            this.solvedGrid = matchModel.gridModel.getBaseGrid();
            new Solver(Strategies.getAll()).solve(this.solvedGrid);

            SudokuGrid.loadNewGrid(matchModel.gridModel, this.solvedGrid);
            this.matchTimer.start(matchModel.elapsedTime);
            EVENTBROKER.matchTopic.trigger(TOPIC_EVENTS.matchStarted, matchModel);
        },

        onTimerTick: function () {
            if (!this.matchModel)
                return;
            this.matchModel.elapsedTime = this.matchTimer.getValue();
            EVENTBROKER.matchTopic.trigger(TOPIC_EVENTS.matchTimerTick, this.matchModel.elapsedTime);
        },

        loadSavedMatch: function () {
            return SaveManager.tryLoadItem("lastMatch", MatchModel);
        },

        saveMatch: function () {
            SaveManager.saveItem("lastMatch", this.matchModel);
        },

        onGridChanged: function () {
            this.saveMatch();
            this._checkMatchCompleted();
        },

        _checkMatchCompleted: function () {
            var completed = HelpProvider.isCompleted(this.matchModel.gridModel, this.solvedGrid);
            if (completed) {
                EVENTBROKER.matchTopic.trigger(TOPIC_EVENTS.matchCompleted, this.matchModel);
                this.clearMatch();
            }
        },

        onRequestNew: function (ev, difficulty) {
            EVENTBROKER.matchTopic.trigger(TOPIC_EVENTS.matchStarting, difficulty);
            setTimeout(function () {
                var generator = new Generator();
                var level = Levels[difficulty];
                var gridModel = generator.createNew(level);
                var matchModel = new MatchModel(gridModel);
                matchModel.difficulty = difficulty;
                this.startMatch(matchModel);
            }.bind(this), 100);

        },

        onRequestPause: function () {
            this.matchTimer.pause();
        },

        onRequestResume: function () {
            this.matchTimer.resume();
        },

        onRequestQuit: function () {
            this.clearMatch();
            EVENTBROKER.matchTopic.trigger(TOPIC_EVENTS.matchEnded, null);
        },

        onRequestRestart: function () {
            var matchModel = new MatchModel(this.matchModel.gridModel.getBaseGrid());
            matchModel.difficulty = this.matchModel.difficulty;

            this.clearMatch();
            this.startMatch(matchModel);
        },

        clearMatch: function () {
            SaveManager.removeItem("lastMatch");
            SudokuGrid.matchEnded = true;
            this.matchModel = null;
            this.matchTimer.clear();
        }
    };

    global.Game.init();

}(this));
